<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { ChallengeApi, propApi } from '@/api/controller';
  import { ToastWarning, Toastloading, allNumber } from '@/utils/public';
  import xmUpload from '@/components/Upload/xm-upload.vue';
  const formLabelWidth = '170px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    item_type:1,
    giftList:[] as Array<any>,
  })
  const options = [
    {value:0,label:'关闭'},
    {value:1,label:'开启'}
  ]
  interface formQuert {
    id:number
    item_name:string
    item_img:string
    gift_id:number
    reward_jd_num:number
    sort:number
    if_rank:number
    if_screen:number
    if_notice:number
    base_rate:number
    low_rate:number
    high_rate:number
    new_protect_rate:number
    day_rate_1:number
    day_rate_2:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    item_name: [
      { required: true, message: '请输入图标名称', trigger: 'blur' }
    ],
    item_img: [
      { required: true, message: '请上传图标', trigger: 'blur' }
    ],
    gift_id: [
      { required: true, message: '请选择礼物', trigger: 'blur' }
    ],
    // reward_jd_num: [
    //   { required: true, message: '请输入奖励金豆数', trigger: 'blur' }
    // ],
    sort: [
      { required: true, message: '请输入排序', trigger: 'blur' }
    ],
    base_rate: [
      { required: true, message: '请输入基础概率', trigger: 'blur' }
    ],
    low_rate: [
      { required: true, message: '请输入低回概率', trigger: 'blur' }
    ],
    high_rate: [
      { required: true, message: '请输入高回概率', trigger: 'blur' }
    ],
    new_protect_rate: [
      { required: true, message: '请输入新人保护概率', trigger: 'blur' }
    ],
    day_rate_1: [
      { required: true, message: '请输入每日保护概率1', trigger: 'blur' }
    ],
    day_rate_2: [
      { required: true, message: '请输入每日保护概率2', trigger: 'blur' }
    ]
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      ChallengeApi.pollEdit(form).then(res=>{
        loading.close()
        closeDialog()
        $emit('updateList')
      }).catch(()=>{
        loading.close()
      })
    })
  }
  const getGiftsList = async()=>{
    await propApi.getGifts({limit:1000}).then((res) => {
      const { data=[] } = res.data
      action.giftList = data
    }).catch(() => {
    });
  }
  const openDialog = async(query:any = {})=> {
    const loading = Toastloading()
    await getGiftsList()
    loading.close()
    const {
      id=0,
      item_name='',
      item_img='',
      gift_id='',
      reward_jd_num='',
      sort='',
      if_rank=0,
      if_screen=0,
      if_notice=0,
      base_rate='',
      low_rate='',
      high_rate='',
      new_protect_rate='',
      day_rate_1='',
      day_rate_2='',
      item_type=1
    } = query
    if(!id) return ToastWarning('角色id not defing')
    Object.assign(form,{
      id,
      item_name,
      item_img,
      gift_id,
      reward_jd_num,
      sort,
      if_rank,
      if_screen,
      if_notice,
      base_rate,
      low_rate,
      high_rate,
      new_protect_rate,
      day_rate_1,
      day_rate_2,
    })
    action.item_type = item_type
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog input-width xm-dialog-cont-y "
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="60%"
    :title="`${form.item_name}人物修改`">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <div class="flex-wrap between">
          <div>
            <el-form-item :label-width="formLabelWidth" label="人物名称" prop="item_name">
              <el-input v-model="form.item_name" />
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="人物图标" prop="item_img">
              <xmUpload :data="{type:6}" listType="picture-card" v-model:file-url="form.item_img" />
            </el-form-item>
            <el-form-item v-if="action.item_type == 1" :label-width="formLabelWidth" label="礼物" prop="gift_id">
              <el-select clearable v-model="form.gift_id" filterable placeholder="请选择">
                <el-option
                  v-for="item in action.giftList"
                  :key="item.gift_id"
                  :label="item.name"
                  :value="item.gift_id">
                </el-option>
              </el-select>
            </el-form-item>
            <!--  prop="reward_jd_num" -->
            <el-form-item :label-width="formLabelWidth" label="奖励金豆数">
              <el-input v-model="form.reward_jd_num" />
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="排序" prop="sort">
              <el-input v-model="form.sort" />
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="上排行榜">
              <el-switch
                v-model="form.if_rank"
                active-color="#13ce66"
                inactive-color="#eee"
                :active-value="1"
                active-text="是"
                :inactive-value="0"
                inactive-text="否">
              </el-switch>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="飘屏">
              <el-switch
                v-model="form.if_screen"
                active-color="#13ce66"
                inactive-color="#eee"
                :active-value="1"
                active-text="是"
                :inactive-value="0"
                inactive-text="否">
              </el-switch>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="页面公告">
              <el-switch
                v-model="form.if_notice"
                active-color="#13ce66"
                inactive-color="#eee"
                :active-value="1"
                active-text="是"
                :inactive-value="0"
                inactive-text="否">
              </el-switch>
            </el-form-item>
          </div>
          <div>
            <el-form-item :label-width="formLabelWidth" label="基础概率(万分位)" prop="base_rate">
              <el-input v-model="form.base_rate" @input="allNumber(form,'base_rate')" />
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="低回概率(万分位)" prop="low_rate">
              <el-input v-model="form.low_rate" @input="allNumber(form,'low_rate')" />
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="高回概率(万分位)" prop="high_rate">
              <el-input v-model="form.high_rate" @input="allNumber(form,'high_rate')" />
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="新人保护概率(万分位)" prop="new_protect_rate">
              <el-input v-model="form.new_protect_rate" @input="allNumber(form,'new_protect_rate')" />
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="每日保护概率1(万分位)" prop="day_rate_1">
              <el-input v-model="form.day_rate_1" @input="allNumber(form,'day_rate_1')" />
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="每日保护概率2(万分位)" prop="day_rate_2">
              <el-input v-model="form.day_rate_2" @input="allNumber(form,'day_rate_2')" />
            </el-form-item>
          </div>
        </div>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>